package com.hm.hiedict.slice;

import com.hm.hiedict.ResourceTable;
import com.hm.hiedict.net.HiWeb;
import com.hm.hiedict.sql.HiSql;
import com.hm.hiedict.util.HiExecutor;
import com.hm.hiedict.util.HiSearchWordCallback;
import com.hm.hiedict.util.HiWordData;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.system.DeviceInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private HiLogLabel hiLogLabel = new HiLogLabel(0, 0, "HiEdict");
    private HiSql hiSql;
    private TextField textFieldWord;
    private Text textSearchResult;
    private Image imageSearch;
    private Image imageShow;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        if (DeviceInfo.getDeviceType().equals("tv")){
            super.setUIContent(ResourceTable.Layout_hiedict_tv_main);
        } else {
            super.setUIContent(ResourceTable.Layout_hiedict_wearable_main);
        }

        hiSql = new HiSql(this);
        try{
            hiSql.init();
        } catch (IOException e) {
            HiLog.error(hiLogLabel, "Exception,msg:" + e.toString());
            e.printStackTrace();
            terminateAbility();
        }

        if (DeviceInfo.getDeviceType().equals("tv")){
            initLayoutTv();
        } else {
            initLayoutWearable();
        }

    }

    private void initLayoutWearable() {
        textFieldWord = (TextField)findComponentById(ResourceTable.Id_text_field_wearable);
        textSearchResult = (Text)findComponentById(ResourceTable.Id_text_search_show_result_wearable);
        imageSearch = (Image)findComponentById(ResourceTable.Id_image_search_wearable);
        if (imageSearch != null){
            imageSearch.setClickable(true);
            imageSearch.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    handleTextFieldAndShow();
                }
            });
        }
    }

    private void initLayoutTv() {
        textFieldWord = (TextField)findComponentById(ResourceTable.Id_text_field_input_dict);
        textSearchResult = (Text)findComponentById(ResourceTable.Id_text_search_show_result);
        imageSearch = (Image)findComponentById(ResourceTable.Id_image_search);
        imageShow = (Image)findComponentById(ResourceTable.Id_image_bg_show);
        if (imageSearch != null){
            imageSearch.setClickable(true);
            imageSearch.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    imageShow.setVisibility(Component.HIDE);
                    textSearchResult.setVisibility(Component.VISIBLE);
                    handleTextFieldAndShow();
                }
            });
        }
    }

    private void handleTextFieldAndShow(){
        String word = textFieldWord.getText();
        ArrayList<HiWordData> result = hiSql.searchLocalDict(word);
        if (result.size() > 0){
            textSearchResult.setText(word + ":\r\n");
            // 输出查询结果
            for (HiWordData hiWordData:result){
                textSearchResult.append(hiWordData.type
                        + " " + hiWordData.meanings + "\r\n");
            }
        } else {
            textSearchResult.setText("本地词库没有查找到此单词，正在网络查找...");
            getWordFromWeb(word);
        }
    }

    private class HiSearchWordCallbackImpl implements HiSearchWordCallback{

        @Override
        public void onSuccessResult(String word, List<HiWordData> result) {
            HiExecutor.runUI(new Runnable() {
                @Override
                public void run() {
                    updateTextResult(word, result);
                }
            });
        }

        @Override
        public void onFailResult(String msg) {
           HiExecutor.runUI(new Runnable() {
               @Override
               public void run() {
                   textSearchResult.setText("网络获取单词失败:" + msg);
               }
           });
        }

        private void updateTextResult(String word, List<HiWordData> result){
            if (result.size() > 0){
                textSearchResult.setText(word + ":\r\n");
                for (HiWordData hiWordData:result){
                    textSearchResult.append(hiWordData.type
                            + " " + hiWordData.meanings + "\r\n");
                }
            } else {
                textSearchResult.setText("网络没有找到此单词，请确认是否输入有误！");
            }
        }
    }

    private void getWordFromWeb(String word) {
        HiSearchWordCallbackImpl hiSearchWordCallback = new HiSearchWordCallbackImpl();
        HiWeb hiWeb = new HiWeb(word, hiSql.getStore(), hiSearchWordCallback);
        hiWeb.getWebWord();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
